package demo.DataStrucAndAlgo.SingletonPattern.Prototype;

import java.util.Hashtable;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/8/13-08-13-17:10
 * @Description：demo.DataStrucAndAlgo.SingletonPattern.Prototype
 */
public class ShapeCache {
    private static Hashtable<String, Shape> shapeMap =
            new Hashtable<>();
    public static Shape getShape(String shapeId){
        Shape cacheShape = shapeMap.get(shapeId);
        return (Shape) cacheShape.clone();
    }

    //对每种形状都运行数据库查询，并创建该形状
    //shapeMap.put(shapeKey,shape);
    //例如，我们要添加三种形状
    public static void loadCache(){
        Circle circle = new Circle();
        circle.setId("1");
        shapeMap.put(circle.getId(),circle);

        Square square = new Square();
        square.setId("2");
        shapeMap.put(square.getId(), square);

        Rectangle rectangle = new Rectangle();
        rectangle.setId("3");
        shapeMap.put(rectangle.getId(),rectangle);
    }
}
